package com.yuwj.chinaa.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.entity.EntityPartnerApply;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.OnClick;

public class PartnerApplyActivity extends BaseActivity{
    @BindView(R.id.tv_total_assets)
    TextView tvAssetsTotal;
    private AlertDialog dialogWithPayPasswordInput;
    IPresenter presenter;

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_partner_apply;
    }

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, PartnerApplyActivity.class);
        return intent;
    }

    @Override
    public void initParam() {
        presenter=new PresenterImpl(this);

    }

    @Override
    public void initView() {
        tvAssetsTotal.setText(entityUser.getBalance()+"  china-A");

    }

    @Override
    public void initOperator() {

    }

    @OnClick({R.id.iv_back,R.id.tv_apply_yew,R.id.tv_apply_shiy,R.id.tv_apply_dongs})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.iv_back:
                onBackPressed();
                break;
            case R.id.tv_apply_yew:
                showConfirmApplyDialog(20140004);
                break;
            case R.id.tv_apply_shiy:
                showConfirmApplyDialog(20140005);
                break;
            case R.id.tv_apply_dongs:
                showConfirmApplyDialog(20140006);
                break;
        }
    }

    public void showConfirmApplyDialog(final int type){
            AlertDialog.Builder builderWithPay = new AlertDialog.Builder(context);
            LinearLayout layout = (LinearLayout) getLayoutInflater().inflate(R.layout.content_confirm_apply_partner, null);
            LinearLayout llDismiss = (LinearLayout) layout.findViewById(R.id.ll_dismiss);
            TextView tvConfirm = (TextView) layout.findViewById(R.id.tv_confirm);
            TextView tvCancel=(TextView) layout.findViewById(R.id.tv_cancel);
            View.OnClickListener onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (view.getId()) {
                        case R.id.ll_dismiss:
                            dialogWithPayPasswordInput.dismiss();
                            break;
                        case R.id.tv_confirm:
                            JSONObject param=new JSONObject();
                            try {
                                param.put("assetPackageType",type+"");
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                            presenter.addPartnerApply(param.toString());
//
                            dialogWithPayPasswordInput.dismiss();
                            break;
                        case R.id.tv_cancel:
                            dialogWithPayPasswordInput.dismiss();
                            break;

                    }
                }
            };
            llDismiss.setOnClickListener(onClickListener);
            tvCancel.setOnClickListener(onClickListener);
            tvConfirm.setOnClickListener(onClickListener);
            dialogWithPayPasswordInput = builderWithPay.setView(layout).create();
            dialogWithPayPasswordInput.setCanceledOnTouchOutside(false);
            dialogWithPayPasswordInput.show();
    }

    @Override
    public void onAddPartnerApplySuccess(EntityPartnerApply entityPartnerApply) {
        super.onAddPartnerApplySuccess(entityPartnerApply);
        ToastUtil.getInstance().shortLength(context,"申请成功");
    }

    @Override
    public void onAddPartnerApplyFail(Exception e) {
        super.onAddPartnerApplyFail(e);
        ToastUtil.getInstance().shortLength(context,e.getMessage());
    }
}
